import 'package:flutter/cupertino.dart';
import 'package:hippo_app/pages/login/login.dart';
import 'package:hippo_app/pages/plant/plant_category.dart';
import 'package:hippo_app/pages/plant/plant_category_manage.dart';
import 'package:hippo_app/pages/plant/plant_manage.dart';
import 'package:hippo_app/pages/plant/plant_view.dart';
import 'package:hippo_app/pages/setting/setting.dart';

import 'pages/app/app.dart';
import 'pages/garden/garden_name.dart';
import 'pages/home/home.dart';
import 'pages/error/not_found.dart';
import 'pages/launch_screen/launch_screen.dart';

class RouterTable {
  static String appPath = '/app';
  static String loginPath = '/login';
  static String homePath = '/home';
  static String notFoundPath = '/notFound';

  static Widget launchScreenPage = const LaunchScreenPage();
  static Widget homePage = const HomePage();
  static Widget settingPage = const SettingPage();
  static Widget plantCategoryPage = const PlantCategoryPage();

  static List<String> whitelist = ['/login', '/notFound'];

  static Map<String, WidgetBuilder> routeTables = {
    "/app": (context) => const AppPage(),
    "/notFound": (context) => const NotFoundPage(),
    "/home": (context) => const HomePage(),
    "/login": (context) => const LoginPage(),
    "/plantView": (context, {arguments}) => PlantViewPage(arguments: arguments),
    "/gardenName": (context, {arguments}) => GardenNamePage(),
    "/plantCategoryManage": (context, {arguments}) => PlantCategoryManagePage(),
    "/plantManagePage": (context, {arguments}) => PlantManagePage(),
  };

  static Route onGenerateRoute<T extends Object>(RouteSettings settings) {
    String routeName = routeBeforeHook(settings);
    final Function? pageBuilder = routeTables[routeName];
    if (routeName == notFoundPath) {
      return CupertinoPageRoute(builder: (context) => pageBuilder!(context));
    }

    if (null != pageBuilder) {
      return CupertinoPageRoute(
        builder: (context) {
          return null != settings.arguments
              ? pageBuilder(context, arguments: settings.arguments)
              : pageBuilder(context);
        },
        settings: settings,
      );
    } else {
      return CupertinoPageRoute(builder: (context) => pageBuilder!(context));
    }
  }

  static String routeBeforeHook(RouteSettings settings) {
    String name = settings.name!;

    if (routeTables[name] == null) {
      return notFoundPath;
    }

    if (whitelist.indexOf(name) > 0) {
      return name;
    }

    /*final token = StoreUtil.getToken();
    if (token != '') {
      return name;
    }*/

    return name;
    //return loginPath;
  }
}
